package com.example.myapplication

import android.content.Intent
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.myapplication.banner.BannerItem
import com.example.myapplication.banner.BannerItemType
import com.example.myapplication.banner.BannerView
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.utils.GSYVideoType

class MainActivity : AppCompatActivity() {
    
    private lateinit var bannerView: BannerView
    private var isBannerInitialized = false
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        findViewById<TextView>(R.id.tv_jump).setOnClickListener {
//            startActivity(Intent(this@MainActivity,TestActivity::class.java))
            resetBannerData()
        }
        initBanner()
    }
    
    private fun initBanner() {
        GSYVideoType.setShowType(GSYVideoType.SCREEN_MATCH_FULL);
        // 获取XML中定义的BannerView
        bannerView = findViewById(R.id.bannerView)
        
        // 创建Banner数据
        val bannerData = listOf(
            BannerItem(BannerItemType.IMAGE, "https://pic4.zhimg.com/v2-7f5e7ec5909bc70c37badc889ed52089_1440w.jpg"),
            BannerItem(BannerItemType.VIDEO, "https://www.w3schools.com/html/mov_bbb.mp4"), // 网络视频URL示例
            BannerItem(BannerItemType.IMAGE, "https://pic1.zhimg.com/v2-1ef95ba71ce4e5a1f14401047efc5d36_1440w.jpg"),
            BannerItem(BannerItemType.VIDEO, "https://www.w3schools.com/html/mov_bbb.mp4"), // 网络视频URL示例
            BannerItem(BannerItemType.IMAGE, "https://pic4.zhimg.com/v2-09892b017cb86cd9b4f3c424523b518b_1440w.jpg")
        )
        
        // 设置Banner数据
        bannerView.setBannerData(bannerData)
        
        // 开始自动轮播
        bannerView.startAutoPlay()
        isBannerInitialized = true
    }
    
    // 添加重新设置Banner数据的方法示例
    private fun resetBannerData() {
        if (!isBannerInitialized) {
            initBanner()
            return
        }
        // 停止当前播放
        bannerView.stopAutoPlay()
        // 创建新的Banner数据
        val newData = listOf(
            BannerItem(BannerItemType.IMAGE, "https://pic4.zhimg.com/v2-7f5e7ec5909bc70c37badc889ed52089_1440w.jpg"),
            BannerItem(BannerItemType.IMAGE, "https://pic1.zhimg.com/v2-1ef95ba71ce4e5a1f14401047efc5d36_1440w.jpg"),
            BannerItem(BannerItemType.VIDEO, "https://www.w3schools.com/html/mov_bbb.mp4") // 网络视频URL示例
        )
        // 重新设置Banner数据
        bannerView.setBannerData(newData)
        // 重新开始自动轮播
        bannerView.startAutoPlay()
    }
    
    override fun onResume() {
        super.onResume()
        if (isBannerInitialized) {
            bannerView.resume()
        }
    }
    
    override fun onPause() {
        super.onPause()
        if (isBannerInitialized) {
            bannerView.pause()
        }
    }
    
    override fun onDestroy() {
        super.onDestroy()
        if (isBannerInitialized) {
            bannerView.release()
        }
        GSYVideoManager.releaseAllVideos()
    }
}